/*
 * UART.c
 *
 * Created: 28/11/2011 10:36:32
 *  Author: Dani
 */ 

#include <avr/io.h>
#define F_CPU 8000000
#include <util/delay.h>
#include <avr/interrupt.h>
#include "uart.h"
#include "lcd162.h"
#include "switch.h"

uint8_t flag = 0;
uint8_t counter = 0;

int main()
{
	char ch;
	char TestString[40] = "This string is stored in SRAM memory\r\n";

	  // Initialize USART
	  // Important : Also other Baud Rates and Data Bits should be tested
	  // Corresponding changes has to be made in the terminal program used
	  // for the test
	InitUART(9600, 8);
	
	LCDInit();
	LCDDispString("UART");
	LCDGotoXY(0,1);
	_delay_ms(1000);
	
	SwitchInit();
	sei();
	  
  	DDRA = 0b11111000;
	PORTA = 0;

	while(1)
	{	
		LCDGotoXY(6,0);
		LCDDispInteger(counter);
		_delay_ms(100);
		
		if (flag == 1)
		{
			LCDGotoXY(0,1);
			LCDDispString("Sending");
			
			// Testing SendChar()
			SendChar('a');
			SendChar('B');
			SendChar('C');
			SendChar('w');
			SendChar('\r');
			SendChar('\n');

			// Testing ReadChar() : Read and echo
			ch = ReadChar();
			SendChar(ch);
			SendChar('\r');
			SendChar('\n');

			// Testing CharReady()
			while ( !CharReady() )
			{}
			SendChar( ReadChar() );
			SendChar('\r');
			SendChar('\n');
    
			// Sending string stored in Flash (constant string)
			SendString("This string is stored as a constant in flash memory\r\n");
    
			// Sending string stored in SRAM (modify-able string)
			SendString(TestString);
        
			// Testing SendInteger()
			SendInteger(12345);
			SendChar('\r');
			SendChar('\n');    
			SendInteger(-1000);
			SendChar('\r');
			SendChar('\n');
			
			LCDGotoXY(0,1);
			LCDDispString("Sent    ");
			flag = 0;
		}		    
	}
}
ISR(S1)
{
	counter ++;
	flag = 1;
}